package com.qfedu.openplateform.manager.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.openplateform.manager.dao.AppInfoMapper;
import com.qfedu.openplateform.manager.dao.CustomerMapper;
import com.qfedu.openplateform.manager.entity.AppInfo;
import com.qfedu.openplateform.manager.entity.Customer;
import com.qfedu.openplateform.manager.entity.TableData;
import org.apache.ibatis.annotations.Param;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/sys/app_info")
public class AppInfoController {

    @Autowired
    private AppInfoMapper appInfoMapper;
    @Autowired
    private CustomerMapper customerMapper;

    @RequestMapping("/table")
    public TableData findAllAppInfo(int page, int limit,AppInfo appInfo) {

        TableData<AppInfo> TableData = new TableData();

        List<AppInfo> customers = appInfoMapper.findAllAppInfo();

        PageHelper.startPage(page, limit);

        PageInfo pageInfo = new PageInfo(customers);

        TableData.setCount(pageInfo.getTotal());

        TableData.setData(customers);

        List<AppInfo> appInfoByName = appInfoMapper.findAppInfoByName(appInfo);

        PageHelper.startPage(page, limit);

        PageInfo pageInfo1 = new PageInfo(appInfoByName);

        TableData.setCount(pageInfo1.getTotal());

        TableData.setData(appInfoByName);

        return TableData;
    }


     @RequestMapping("/del")
     public TableData deleteAppInfo(Integer ids){

         TableData<Object> TableData = new TableData<>();

         int i = appInfoMapper.deleteAppInfo(ids);

         if (i > 0) {

             TableData.setCode(1);

             TableData.setMsg("删除成功");
         }else {

             TableData.setMsg("删除失败");
         }
         return TableData;
     }

     @RequestMapping("/add")
     public TableData addInfo(AppInfo appInfo){

         TableData<Customer> TableData = new TableData<>();

          appInfo.setCorpName("正大财团");
         int i = appInfoMapper.addInfo(appInfo);

         if (i > 0) {

             TableData.setCode(1);

             TableData.setMsg("添加成功");
         }else {

             TableData.setMsg("添加失败");
         }
         return TableData;
     }

     @RequestMapping("/update")
     public TableData updateCustomer(AppInfo appInfo) {

         int i = appInfoMapper.updateInfo(appInfo);

         TableData<Object> TableData = new TableData<>();

         if (i > 0) {

             TableData.setCode(1);

             TableData.setMsg("修改成功");
         }else {

             TableData.setMsg("修改失败");
         }

         return TableData;
     }
}